<?php // I18N Internationalisation Class
class X_i18n extends X{
    public $engine;
    public $langs;
    public $texts= null;
    public $db;
    
    public function __construct($db,&$getLang=null){
        $this->db= $db;
        if(X_config::$i18n_cache && isset($_SESSION['X_i18n_cache'])){
            $this->texts= $_SESSION['X_i18n_cache'];
        }
        $str_browser_langs= array();
        $browser_langs= array();
        if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
            $str_browser_langs= preg_split("#;#",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
            $str_browser_langs[0]= strtolower($str_browser_langs[0]);
            $browser_langs= preg_split("#,#",$str_browser_langs[0]);
        }
        $this->langs[]= & $getLang;
        $this->langs[]= & $_SESSION['X_lang'];
        foreach($browser_langs as $bl)if(!in_array($bl, $this->langs) && !empty(X_config::$i18n_langs_active[$bl]))$this->langs[]=$bl;
        foreach(X_config::$i18n_langs_active as $l=>$active)if($active && !in_array($l, $this->langs))$this->langs[]=$l;
        if(!empty($getLang)) $_SESSION['X_lang']= $getLang;
        if(X_config::$i18n_fillCacheBeforeBootstrap)$this->getTexts();
    }
    
    public function __($text,$replace=null){
        if(is_array($this->texts) && array_key_exists($text, $this->texts)){
            $text= $this->texts[$text];
        }elseif($this->texts !==false){
            $this->getTexts();
            if(is_array($this->texts) && array_key_exists($text, $this->texts)){
                $text= $this->texts[$text];
            }
        }
        if(is_array($text)){
            foreach($this->langs as $lang){
                if(!empty($lang) && !empty($text[$lang])){
                    $text= $text[$lang];
                    break;
                }
            }
        }
        if(!empty($text) && $replace!==null){
            if(is_array($replace)){
                foreach($replace as $key=>$str){
                    $text= preg_replace("#%".preg_quote($key,"#")."%#i",$str,$text);
                }
            }else{
                $text= preg_replace("#%.+%#U",$replace,$text);
            }
        }
        return $text;
    }
    
    public function getTexts(){
        if($this->db instanceof X_mysql || (is_array($this->db) && reset($this->db) instanceof X_mysql)){if(is_array($this->db))$this->db=current($this->db);
            $this->engine= "mysql";
            if(empty($this->texts)){
                $mysql= new X_mysql_query(X_config::$i18n_db_table, $this->db->link);
                $this->texts= $mysql->fetchAll(true);
                if(X_config::$i18n_cache && !empty($this->texts)){
                    $_SESSION['X_i18n_cache']= $this->texts;
                }
            }
        }elseif(is_string($this->db) || (is_array($this->db) && is_string(reset($this->db)))){
            $this->engine= "text";
            
        }elseif(is_array($this->db)){
            
        }else{
            
        }
    }
    
}
